package com.ningxia.water.controller;


import com.ningxia.water.entity.TsApplicantInfo;
import com.ningxia.water.entity.TsApplicantWell;
import com.ningxia.water.reuslt.Result;
import com.ningxia.water.reuslt.ResultResponse;
import com.ningxia.water.service.ITsApplicantInfoService;
import com.ningxia.water.service.ITsApplicantWellService;
import com.ningxia.water.utils.CommonUtils;
import com.ningxia.water.vo.out.ApplicantInfoVo;
import com.ningxia.water.vo.out.ApplicantWellVo;
import com.ningxia.water.vo.out.WellInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/applicant")
@Api(tags = "基本信息-用水户API")
public class ApplicantController {

    @Autowired
    private ITsApplicantInfoService applicantInfoService;

    @Autowired
    private ITsApplicantWellService applicantWellService;

    @GetMapping("/getapplicantinfo")
    @ApiOperation("获取用水户信息")
    public Result getApplicantInfo(@RequestParam(value = "applicant_name", required = false) String applicantName,
                                   @RequestParam(value = "zoneId", required = false) Integer zoneId,
                                   @RequestParam(value = "zoneType", required = false) Integer zoneType) {
        List<ApplicantInfoVo> list = applicantInfoService.getApplicantInfo(applicantName, zoneId, zoneType);
        return ResultResponse.getSuccessResult(list);
    }

    @GetMapping("/getallapplicants")
    @ApiOperation("获取全部用水户")
    public Result getAllApplicants() {
        List<TsApplicantInfo> list = applicantInfoService.list();
        return ResultResponse.getSuccessResult(list);
    }

    @PostMapping("/insertapplicantinfo")
    @ApiOperation("新增用水户信息")
    public Result insertApplicantInfo(@RequestBody TsApplicantInfo applicantInfo) {
        return CommonUtils.commonResult(applicantInfoService.save(applicantInfo));
    }

    @PutMapping("/updateapplicantinfo")
    @ApiOperation("修改用水户信息")
    public Result updateApplicantInfo(@RequestBody TsApplicantInfo applicantInfo) {
        return CommonUtils.commonResult(applicantInfoService.updateById(applicantInfo));
    }

    @DeleteMapping("/deleteapplicantinfo")
    @ApiOperation("删除用水户信息")
    public Result deleteApplicantInfo(@RequestParam Integer applicantId) {
        return CommonUtils.commonResult(applicantInfoService.removeById(applicantId));
    }

    @GetMapping("/getwellinfos")
    @ApiOperation("获取该用户下的所有机井")
    public Result getWellInfos(@RequestParam Integer applicantId) {
        List<ApplicantWellVo> list = applicantWellService.getWellInfos(applicantId);
        return ResultResponse.getSuccessResult(list);
    }


    /**
     * 分配机井
     */
    @PostMapping("/assignwell")
    @ApiOperation(value = "将机井分配给指定用户")
    public Result assignWellToApplicant(@RequestBody List<TsApplicantWell> list) {
        return CommonUtils.commonResult(applicantWellService.saveBatch(list));
    }
}
